<?php

namespace App\Services\Admin\Sponsor;

use Exception;

class UserIntegralService extends BaseSponsorService
{

    /**
     * 发放积分记录列表
     * @param array $params
     * @return array[]
     * @throws Exception
     */
    public function list(array $params): array
    {
        // 总条数
        $total = $this->getSponsorIntegralRecordsModel()->total($params['sponsorId']);

        // 列表
        $list = $this->getSponsorIntegralRecordsModel()->getItemsBySponsorId($params['sponsorId'], [
            'id', 'sponsor_id', 'user_integral_id'
        ]);

        $userIntegralIds = array_column($list, 'user_integral_id');

        // 查询积分记录列表
        $userIntegralList = $this->getUserIntegralRecordsModel()->getItemsByIds($userIntegralIds, [
            'id', 'quantity', 'remark','created_at'
        ]);

        $list = [];
        foreach ($userIntegralList as $item) {
            $list[] = [
                'id' => $item['id'],
                'quantity' => $item['quantity'],
                'remark' => $item['remark'],
                'createdAt' => $item['created_at']
            ];
        }

        return [
            'total' => $total,
            'list' => $list,
        ];
    }
}
